﻿using RabbitMQ.Client;
using RabbitMQ.Client.Events;
using System;
using System.Text;
using System.Threading;

namespace RabbitMQReceive
{
    class Program
    {
        static void Main(string[] args)
        {
            var factory = new ConnectionFactory() { HostName = "192.168.0.201" };
            using (var connection = factory.CreateConnection())
            {
                using (var channel = connection.CreateModel())
                {
                    #region 基础
                    channel.QueueDeclare(queue: "hello", durable: false, exclusive: false, autoDelete: false, arguments: null);

                    var consumer = new EventingBasicConsumer(channel);
                    consumer.Received += (model, ea) =>
                    {
                        var body = ea.Body;
                        var message = Encoding.UTF8.GetString(body);
                        Console.WriteLine("1 Received {0}", message);
                    };
                    channel.BasicConsume(queue: "hello", autoAck: true, consumer: consumer);
                    #endregion

                    #region 工作队列模式(Work Queue) https://www.rabbitmq.com/tutorials/tutorial-two-dotnet.html
                    channel.QueueDeclare(queue: "task_queue", durable: true, exclusive: false, autoDelete: false, arguments: null);

                    channel.BasicQos(prefetchSize: 0, prefetchCount: 1, global: false);

                    var consumer2 = new EventingBasicConsumer(channel);
                    consumer2.Received += (model, ea) =>
                    {
                        var body = ea.Body;
                        var message = Encoding.UTF8.GetString(body);
                        Console.WriteLine("2 Received {0}", message);

                        channel.BasicAck(deliveryTag: ea.DeliveryTag, multiple: false);
                    };

                    channel.BasicConsume(queue: "task_queue", autoAck: false, consumer: consumer2);
                    #endregion

                    #region 发布/订阅模式(Publish/Subscribe) https://www.rabbitmq.com/tutorials/tutorial-three-dotnet.html
                    channel.ExchangeDeclare(exchange: "logs", type: "fanout");

                    var queueName = channel.QueueDeclare().QueueName;
                    channel.QueueBind(queue: queueName, exchange: "logs", routingKey: "");

                    var consumer3 = new EventingBasicConsumer(channel);
                    consumer3.Received += (model, ea) =>
                    {
                        var body = ea.Body;
                        var message = Encoding.UTF8.GetString(body);
                        Console.WriteLine("3 Received {0}", message);
                    };
                    channel.BasicConsume(queue: queueName, autoAck: true, consumer: consumer3);
                    #endregion


                    channel.ExchangeDeclare(exchange: "direct_logs", type: "direct");
                    var queueName2 = channel.QueueDeclare().QueueName;

                    channel.QueueBind(queue: queueName2, exchange: "direct_logs", routingKey: "info");
                    channel.QueueBind(queue: queueName2, exchange: "direct_logs", routingKey: "warn");


                    var consumer4 = new EventingBasicConsumer(channel);
                    consumer4.Received += (model, ea) =>
                    {
                        var body = ea.Body;
                        var message = Encoding.UTF8.GetString(body);
                        var routingKey = ea.RoutingKey;
                        Console.WriteLine(" [x] Received '{0}':'{1}'",
                                          routingKey, message);
                    };
                    channel.BasicConsume(queue: "info", autoAck: true, consumer: consumer4);
                    channel.BasicConsume(queue: "warn", autoAck: true, consumer: consumer4);





                    Console.ReadLine();
                }
            }

            Console.ReadKey();
        }
    }
}
